<?php

class Guidance_DetailController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->yearSemester = $this->_getParam('yearSemester');
        $this->view->setYearSemester = $this->_getParam('setYearSemester');

        $this->view->classSn = $this->_getParam('classSn');
        $this->view->studentSn = $this->_getParam('studentSn');
        $form = new Guidance_Form_Guidance();
        $this->view->form = $form;
    }

    public function copyAction()
    {
        $tempArray = explode('-', $this->_getParam('yearSemester'));
        $cYear = (int) $tempArray[0];
        $cSemester = (int) $tempArray[1];
        if ($cSemester == 2) {
            $year = $cYear;
            $semester = 1;
        }
        else {
            $year = $cYear-1;
            $semester = 2;
        }

        $studentSn = (int) $this->_getParam('studentSn');
        $setSn = (int) $this->_getParam('setSn');

        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentGuidance')
        ->where('year=?', $year)
        ->andWHere('semester=?', $semester)
        ->andWhere('student_sn=?', $studentSn);
        $res  = $q->fetchOne();
       // echo $year.'-'. $semester.'-'.$studentSn;
        if ($res) {
            $data = $res->toArray();
            unset($data['sn']);
            $data['year'] = $cYear;
            $data['semester'] = $cSemester;
            $guidance = new Student_Model_StudentGuidance();
            if ($setSn) {
                $guidance->assignIdentifier($setSn);
            }
            $guidance->fromArray($data);
            $guidance->save();
            $this->_helper->json(1);
        }
        else
        $this->_helper->json(0);
    }
    /**
     * 儲存
     */
    public function saveAction()
    {
        if ($this->getRequest()->isPost()) {
            $form = new Guidance_Form_Guidance();
            if ($form->isValid($this->_getAllParams())) {
                $guidance = new Student_Model_StudentGuidance();
                $sn = (int) $this->_getParam('sn');
                $studentSn = (int) $this->_getParam('studentSn');
                $tempArr = explode('-', $this->_getParam('setYearSemester'));
                $guidance->year = (int) $tempArr[0];
                $guidance->semester = (int) $tempArr[1];
                $guidance->student_sn = $studentSn;
                if ($sn)
                    $guidance->assignIdentifier($sn);

                foreach ($form->guidanceSelect as $val)
                $guidance->{$val} = $form->getValue($val);

                $systemText = new Default_Model_SystemText();

                foreach($form->optionArray as $key=>$val) {
                    $tempArray = array();
                    foreach ($systemText->getKindArray($val) as $k=>$v) {
                        if ($form->getValue($key.'_'.$k) == 1)
                            $tempArray[] = $k;
                    }
                    if ($tempArray)
                    $guidance->{$key} = ','.  implode(',', $tempArray).',';
                    else
                     $guidance->{$key} = null;
                }
                $guidance->save();
                 $this->_helper->json(1);

            }
            else
            $this->_helper->json($form->getErrorMessages());
        }

    }

    public function listDataAction()
    {
        $studentSn = (int)$this->_getParam('studentSn');
        $setYearSemester = $this->_getParam('setYearSemester');
        $tempArr = explode('-', $setYearSemester);
        $year = (int) $tempArr[0];
        $semester = (int) $tempArr[1];
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentGuidance')
        ->where('year=?', $year)
        ->andWhere('semester=?', $semester)
        ->andWhere('student_sn=?', $studentSn);
        $res = $q->fetchOne();
        if ($res) {
            $this->_helper->json($res->toArray()) ;
        }
        else
        $this->_helper->json(0);
    }
}

